const express = require('express')
const app = express()
const handleError = require('./middleware/handleError.js')
const cors = require('cors')
const path = require('path')

const testRouter = require('./router/test/index.js')
const recommendRouter = require('./router/recommend/index.js')

// app.use(cors({ 
//   origin: 'http://127.0.0.1',
//   methods: ['GET', 'POST', 'OPTIONS'], // 指定允许的HTTP方法
//   allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With'], // 指定允许的请求头
//   maxAge: 86400 // 预检请求的结果缓存24小时（86400秒）
// }))

app.use(cors())

// 配置静态文件服务，使前端能够访问db目录下的资源
app.use('/banners', express.static(path.join(__dirname, 'db/banners')))
app.use('/playlist', express.static(path.join(__dirname, 'db/playlist')))
app.use('/images', express.static(path.join(__dirname, 'db/images')))

// 配置解析表单数据的中间件
app.use(express.urlencoded({ extended: false }))
app.use(express.json())

// 错误处理中间件
app.use(handleError)

app.use('/test', testRouter)
app.use('/recommend', recommendRouter)

const port = 9000
app.listen(port, () => {
  console.log(`服务运行在 ${port} 端口`)
})
